<?php
function cache_killer($src)
{
	$ft = filemtime($src);
	return '/'.$src.'?'.$ft;
}

$in_bricabox = (bool)@$BBX['in_bbx'] ? true : false;

function google_maps_key()
{
	global $BBX, $in_bricabox;
	if ($in_bricabox || @$BBX['custom_domain'])
	{
		return $BBX['bricabox']->api_key_google_maps;
	}
	elseif (isset($BBX['app_domain']))
	{
		return $BBX['app_domain']->google_maps_key;
	}
}

// navigation tab helpers
$nav_tabs = array(
	'/' 	   => 'Index',
	'/entries' => 'All '.$this->bricabox->label_for_entry_tab(),
	'#new'	   => '+ Add '.$this->bricabox->label_for_entry_tab(true),
	'/members' => 'Members',
	'/admin'   => 'Admin'
);
$__entry_tab_label = $this->bricabox->label_for_entry_tab();
$admin_tabs = array(
	'/admin/general' 		 => 'General settings',
	'/admin/entry_templates' => 'Manage entry templates',
	'/admin/entries' 		 => 'Manage entries',
	'/admin/members' 		 => 'Manage members &amp; invites',
	'/admin/layout' 		 => 'Edit your blocks &amp; layout',
	'/admin/stats' 			 => 'Stats'
);

$in_admin	= preg_match("/admin/", $_SERVER['QUERY_STRING']) ? true : false;
$in_global 	= (!$in_bricabox || preg_match("/(log_(in|out)|auth)/", $_SERVER['QUERY_STRING'])) ? true : false;

$replace = array();

function render_tabs($tabs)
{
	if (count($tabs))
	{
		global $BBX;
		global $replace;
		global $__entry_tab_label;
		
		$CI =& get_instance();
		$CI->load->model('Membership_model');
		
		// get the current tab
		$current_tab = '/'.$_SERVER['QUERY_STRING'];
		
		// override the current tab for entry-related pages
		$override_current = preg_match('/\/(content|entries|entry[^_])/', $current_tab) ? true : false;
		
		foreach ($tabs as $path => $label)
		{
			$label = str_replace('&amp;', 'and', $label);
			if ($path == '#new')
				$id = "add";
			else
				$id = url_title($label, 'underscore');
			
			if ($override_current && $path == '/entries')
				$klass = ' class="current"';
			else
			{
				$path_regexp = str_replace('/', '\/', $path);
				$klass = (!$override_current
					&& ($current_tab == $path || preg_match("/{$path_regexp}/", $current_tab))) ? ' class="current"' : '';
					
				if (strlen($current_tab) > 1 && $path == '/') // more than just /
					$klass = ''; // don't make Index current if another tab matches
			}
			
			// if this is the add button, we have to add some extra js
			$extras = "";
			if ($id == 'add')
			{
				$extras = " title=\"Add a new entry\" onclick=\"return Entry.showNewPanel();\"";
			}
			
			/*if (!preg_match('/^ad/', $id) || (preg_match('/^ad/', $id) && $CI->userland->logged_in()))*/
				if ($id != 'admin'
					|| ($id == 'admin'
						&& $CI->userland->logged_in()
						&& $CI->Membership_model->is_owner($BBX['bricabox']->id, $CI->userland->current_user()->id))
					)
					if ($id != 'add'
						|| ($id == 'add'
							/*&& $CI->Membership_model->is_member($BBX['bricabox']->id, $CI->userland->current_user()->id)*/
							&& !preg_match('/content\/\d+\/edit/', $current_tab)))
						echo "<li id=\"nav_{$id}\"{$klass}><a href=\"{$path}\"{$extras}><span>{$label}</span></a></li>\n";
						
						
			/**
			 * Whacky tab replacement
			 */
			$id 			= preg_replace('/all_(.*)/', 'all_entries', $id);
			$_replace_klass = $id . '_' . str_replace(' class="current"', 'current', $klass);
			$_replace_klass = preg_replace('/_$/', '_off', $_replace_klass);
			$replace[$id] 	= str_replace("{$id}_", '', $_replace_klass);
		}
	}
}

// load tags for injection into meta keywords
$_tags_str = '';
if ($in_bricabox)
{
	$_tags_query = $this->db->query('select * from contents_tags
		inner join tags on tags.id=contents_tags.tag_id
		where contents_tags.content_id in (select id from contents where bricabox_id = ?)
		group by contents_tags.tag_id
		order by tags.name asc',
		array($BBX['bricabox']->id));
	if ($_tags_query->num_rows())
	{
		foreach ($_tags_query->result() as $_tag_r)
			$_tags_str .= "{$_tag_r->name} ";
	}	
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<meta name="keywords" content="<?php if (@$bricabox && (!$in_admin && !$in_global) && $in_bricabox): ?><?php echo join(', ',explode(' ', $_tags_str.strip_tags($bricabox->name).' '.strip_tags($bricabox->description))).', '; ?><?php endif; ?>bricabox, web host, publishing, blog, blog site, wiki, group website, collaboration, website builder, consumer reviews, make your own site, podcasts, videos, mashups, semantic web" />
	<meta name="description" content="<?php if (@$bricabox && (!$in_admin && !$in_global) && $in_bricabox): ?><?php echo strip_tags($bricabox->name).': '.strip_tags($bricabox->description); ?><?php else: ?>We've taken the best of blogs and social networks, added some amazing new tools and features, and created a platform for you to create your own version of your favorite social content website. Choose from our one-click selection of BricaBoxes to get started.<?php endif; ?><?php if (((!$in_admin && !$in_global) && $in_bricabox) && isset($content->id)) echo " {$content->field1_value}"; ?>" />
	<meta name="author" content="BricaBox, LLC" />
	<meta name="powered-by" content="BricaBox" />
	<meta name="developers" content="Ev Lester" />
	
	<?php if (@$bricabox && (!$in_admin && !$in_global) && $in_bricabox): ?>
	<link rel="alternate" href="/entries/as/rss" type="application/rss+xml" />
	<?php endif; ?>

	<title><?php echo isset($page_title) ? "{$page_title} - " : ''; ?>BricaBox</title>
	
	<link rel="stylesheet" href="<?php echo cache_killer("assets/css/reset.css"); ?>" type="text/css" media="all" />
	<link rel="stylesheet" href="<?php echo cache_killer("assets/css/jquery.suggest.css"); ?>" type="text/css" media="all" />
	<link rel="stylesheet" href="<?php echo cache_killer("colourmod/ColourModStyle.css"); ?>" type="text/css" media="screen, projection" />
	
	<?php if ((!$in_admin && !$in_global) && $in_bricabox): // don't include for admin or login/logout screens ?>
	
	
	<link rel="stylesheet" href="/assets/css/custom_css/<?php echo time(); ?>" type="text/css" media="screen, projection" />
	
	
	
	
	<link rel="stylesheet" href="<?php echo cache_killer("assets/css/naughty.css"); ?>" type="text/css" media="screen, projection" />
	<?php else: ?>
	<link rel="stylesheet" href="<?php echo cache_killer("assets/css/global.css"); ?>" type="text/css" media="screen, projection" />
	<?php endif; ?>
	
	<link rel="shortcut icon" href="favicon.png" type="image/png" />
	
	<script type="text/javascript" charset="utf-8" src="<?php echo "/assets/js/jquery-1.2.1.min.js"; ?>"></script>
	<script type="text/javascript" charset="utf-8" src="<?php echo cache_killer("assets/js/jquery.suggest.js"); ?>"></script>
	<script type="text/javascript" charset="utf-8" src="<?php echo cache_killer("assets/js/app.js"); ?>"></script>
	<script type="text/javascript" charset="utf-8" src="<?php echo cache_killer("assets/js/maps.js"); ?>"></script>
	<script type="text/javascript" charset="utf-8" src="<?php echo cache_killer("assets/js/interface.js"); ?>"></script>
	<script type="text/javascript" charset="utf-8" src="<?php echo "/colourmod/ColourModScript.js"; ?>"></script>
	<script type="text/javascript" charset="utf-8" src="<?php echo "/colourmod/StyleModScript.js"; ?>"></script>
	<script type="text/javascript" charset="utf-8" src="/assets/js/tiny_mce/tiny_mce.js"></script>
	
	<?php if ($in_bricabox): ?>
	<script type="text/javascript" charset="utf-8">Bricabox.init('<?php echo addslashes($BBX['bricabox']->name); ?>');</script>
	<?php endif; ?>
</head>

<?php
$edit_layout 	 = (@$bricabox && @$in_bricabox && !$in_admin && @$this->Membership_model->is_owner($bricabox->id) && strpos($this->uri->uri_string(), 'edit_layout') !== false) ? true : false;
$can_edit_layout = (@$bricabox && @$in_bricabox && !$in_admin && @$this->Membership_model->is_owner($bricabox->id) && !$edit_layout && 
	(preg_match('/^(\/)$/', '/'.$this->uri->uri_string()) ||
		preg_match('/^(\/content\/\d+(\/.*)?)$/', '/'.$this->uri->uri_string()))

);
?>

<?php
$body_class = '';
if (is_a($this->uri, 'CI_URI') && method_exists($this->uri, 'ruri_string'))
{
	$body_class = preg_replace('/[^a-z0-9]/i', '-', $this->uri->ruri_string());
	$body_class = preg_replace('/^-/', '', $body_class);
	$body_class = preg_replace('/-$/', '', $body_class);
}
?>

<body class="<?php echo $body_class; ?>">
<div id="ColourMod">
	<?php if (isset($colourmod)) @require BASEPATH.'../colourmod/index.php'; ?>
</div>
<?php
$site_status = $this->db->query('select value from meta_settings where name = ? limit 1', array('site_status'))->first_row()->value;
if (strlen($site_status)):
?>
<div id="site_status">
	<?php echo $site_status; ?>
</div>
<style type="text/css" media="screen">
	<!--
	#site_status   { background: #000; color: #fff; padding: 3px; }
	#site_status a { color: #fff; border-color: #fff; }
	#site_status strong { font-weight: bold; }
	-->
</style>
<?php endif; ?>
<div id="whoa_everything" class="<?php if (!@$this_is_homepage && !$edit_layout && @$content->field1_value && @$content->id) echo preg_replace('/[^-a-z0-9]/i', '-', strtolower($this->Content_model->get_content_type($content->content_type_id)->name)); ?>
<?php if (@$this_is_homepage) echo 'homepage_woop2'; ?>">
	<?php if (@!$this_is_homepage): ?>
		
	<?php if (!$in_bricabox
			  || (@$bricabox && $in_bricabox
				  && !in_array($BBX['bricabox']->subdomain, $BBX['white_label']))): ?>
	<div id="bricabar">
		<div id="logo">
			<a href="<?php echo APP_ROOT_URL; ?>"><img src="<?php echo cache_killer("assets/images/bar_logotype.png"); ?>" alt="BricaBox" width="80" height="18" /></a>
			
			<!-- Feature -->
			<?php if (@$bricabox && $in_bricabox && $this->userland->logged_in() && $this->userland->current_user()->is_admin): ?><a class="left_nav clone" href="/bricabox/<?php echo $bricabox->id; ?>/toggle_featured">
				<img title="<?php echo $bricabox->is_featured ? 'Unfeature' : 'Feature'; ?>" src="/assets/images/<?php echo $bricabox->is_featured ? 'unfeature' : 'feature'; ?>.png" alt="Feature/Unfeature" /></a>
			<?php endif; ?>
			
			<!-- Clone -->
			<?php if (@$bricabox && $in_bricabox && !$in_admin && $bricabox->can_clone && $bricabox->user_id != @$this->userland->current_user()->id): ?><a class="left_nav clone" href="/bricabox/<?php echo $bricabox->id; ?>/clone">
				<img src="/assets/images/clone.png" alt="Create a site like this" /></a><?php endif; ?>
				
			<!-- Edit layout -->
			<?php if (@$bricabox && $in_bricabox && !$in_admin && $can_edit_layout && !$edit_layout): ?><a href="<?php if ($this->uri->uri_string() != "") echo '/'.preg_replace('/(\d+)\/(.*)$/', '\1', $this->uri->uri_string()); ?>/edit_layout">
				<img src="/assets/images/edit_layout.png" alt="Edit the layout of this page" /></a><?php endif; ?>
				
			<!-- Help & bug report -->
			<a href="http://help.bricabox.com/"><img src="/assets/images/help.png" alt="Help" /></a>
			<a href="/bugs"><img src="/assets/images/bug.png" alt="Report a bug" /></a>
		</div>
		<div id="nav">
			<?php if ($this->userland->logged_in()): ?>
			<a href="#" id="user_panel_link" class="user" onclick="return Userpanel.open();">Hello <strong><?php echo username($this->userland->current_user()); ?></strong>!</a>
			<?php else: ?>
			<?php if (!@$no_signup_link): ?><a href="/sign_up_or_log_in">sign up or log in</a><?php endif; ?>
			<?php endif; ?>
		</div>
	</div>
	
	<?php require_once APPPATH.'views/layouts/user_panel.php'; ?>
	<?php endif; ?>
	
	<?php endif; ?>
	
	<div id="wrap" onclick="Userpanel.hide();">
		<div id="wrap_inner">
			<?php if ($this->userland->logged_in() && username($this->userland->current_user()) == 'New User'): ?>
			<p style="font-size: 14px; background: #ffb; padding: 3px;">Did you know? You can <a href="/account/edit" style="border-color: #111;">change your name</a> to something nicer looking than <?php echo $this->userland->current_user()->login; ?>!</p>
			<?php endif; ?>
			